﻿namespace REVISIONMAX20
{
    using System;
    using System.Collections.Generic;
    using System.Data.SQLite;
    using System.Text;

    public class Group : ObjectBase
    {
        private int _lastrevision;
        private string _name;

        public Group(SQLiteDataReader reader) : base("groups", reader)
        {
            this._name = reader.GetValue(3).StringValueOrEmpty();
            this._lastrevision = reader.GetValue(4).IntValueOrZero();
        }

        public Group(string pname, SQLiteConnection con) : base("groups", con)
        {
            this._name = pname;
        }

        public StringBuilder SaveOrUpdate()
        {
            StringBuilder errors = new StringBuilder();
            try
            {
                using (SQLiteConnection connection = Common.GetSQLiteConnection())
                {
                    connection.Open();
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
                    {
                        using (SQLiteCommand command = connection.CreateGroupCommand())
                        {
                            this.SaveOrUpdate(command, errors);
                        }
                        if (errors.Length == 0)
                        {
                            transaction.Commit();
                            return errors;
                        }
                        transaction.Rollback();
                        return errors;
                    }
                }
            }
            catch (Exception exception)
            {
                base.Rollback();
                errors.AppendLine(string.Format("Error: {0}", exception.Message));
            }
            return errors;
        }

        public void SaveOrUpdate(SQLiteCommand command, StringBuilder errors)
        {
            if (base.Status != Status.StatusPopulated)
            {
                try
                {
                    base.UpdateCommand(command);
                    command.Parameters["@name"].Value = this._name;
                    command.Parameters["@lastrevision"].Value = this._lastrevision.ToString();
                    if (command.ExecuteNonQuery() <= 0)
                    {
                        errors.AppendLine(string.Format("Error: {0} {1} {2} failed.", base.tablename, base.id, (base.Status == Status.StatusNew) ? "insert" : "update"));
                    }
                    else
                    {
                        if (base.Status == Status.StatusDeleted)
                        {
                            SQLiteCommand command2;
                            using (command2 = command.Connection.CreateCommand())
                            {
                                command.CommandText = string.Format("Delete from `sqlitems` where `parentid` =  {0}  ", base.id);
                                command.ExecuteNonQuery();
                            }
                            using (command2 = command.Connection.CreateCommand())
                            {
                                command.CommandText = string.Format("Delete from `sources` where `parentid` =  {0}  ", base.id);
                                command.ExecuteNonQuery();
                            }
                            using (command2 = command.Connection.CreateCommand())
                            {
                                command.CommandText = string.Format("Delete from `logs` where `parentid` =  {0}  ", base.id);
                                command.ExecuteNonQuery();
                            }
                        }
                        base.Commit();
                        base.Persist();
                    }
                }
                catch (Exception exception)
                {
                    errors.AppendLine(string.Format("Error: {0}", exception.Message));
                }
            }
        }

        public List<ConnectionString> ConnectionStringList
        {
            get
            {
                return Common.GetConnectionStringList(this);
            }
        }

        public int lastrevision
        {
            get
            {
                return this._lastrevision;
            }
            set
            {
                base.IsDirty = value != this._lastrevision;
                this._lastrevision = value;
            }
        }

        public List<Log> LogList
        {
            get
            {
                return Common.GetLogList(this);
            }
        }

        public string name
        {
            get
            {
                return this._name;
            }
            set
            {
                base.IsDirty = value != this._name;
                this._name = value;
            }
        }

        public List<Source> SourceList
        {
            get
            {
                return Common.GetSourceList(this);
            }
        }

        public List<SQLItem> SQLItemList
        {
            get
            {
                return Common.GetSQLItemList(this);
            }
        }
    }
}

